package wid;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Test verifies that Pacifist has not fired its gun.  
 * 
 * @author David Wilkie
 */
public class TestPacifistHasNotFired extends RobotTestBed {

  // True if Pacifist fires a bullet
  boolean hasFired = false;
  
  /**
   * Specifies that SittingDuck and Pacifist are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,wid.Pacifist";
  }
  
  /**
   * This test runs for 10 rounds.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /**
   * Tests whether Pacifist has fired its gun.  Note: for some reason, even when there are no
   * bullets, the TurnEndedEvent says that the robots fire many, many bullets of power 1.0 per
   * round.  I consulted the library but could not find an explanation, so this method checks
   * for bullets w/ power >1.0.
   * 
   * @param event TurnEndedEvent info about the turn
   */
  @Override
  public void onTurnEnded (TurnEndedEvent event) {  
    IBulletSnapshot bullets[] = event.getTurnSnapshot().getBullets();
    IRobotSnapshot pacifistSnapshot = event.getTurnSnapshot().getRobots()[1];
    String robotName = pacifistSnapshot.getName();
    assertEquals("robotName is \"wid.Pacifist*\"", robotName, "wid.Pacifist*");
    // If a bullet's owner index is the same as Pacifist's contestant index, Pacifist has fired.
    for (IBulletSnapshot ibs : bullets) {
      if (ibs.getOwnerIndex() == pacifistSnapshot.getContestantIndex() && ibs.getPower() >= 1.1) {
        this.hasFired = true;
      }        
    }
  }
  
  /**
   * After running all matches, determine if Pacifist has fired.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {  
    assertFalse("The robot has not fired", this.hasFired);
  }  
}
